﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using Server.API.Application.Command.Breed;

namespace Server.API.Application.Handler.Breed
{
    public class UpdateBreedHandler:IRequestHandler<UpdateBreedCommand,APIResult<int>>
    {
        private readonly IRepository<BreedModel> breedway;
        private readonly ILogger<GetBreedOneHandler> logger;
        private readonly IMapper mapper;

        public UpdateBreedHandler(IRepository<BreedModel> breedway, ILogger<GetBreedOneHandler> logger, IMapper mapper)
        {
            this.breedway = breedway;
            this.logger = logger;
            this.mapper = mapper;
        }
        /// <summary>
        /// 养殖信息修改
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<int>> Handle(UpdateBreedCommand request, CancellationToken cancellationToken)
        {
           APIResult <int> res=new APIResult<int>();
            var value=mapper.Map<BreedModel>(request);
            var result=await breedway.Updateway(value);
            if(result>0)
            {
                logger.LogInformation("养殖信息修改成功");
                res.Num=StateCode.OK;
                res.Message="修改成功";
                res.Data=result;
          
            }
            else
            {
                logger.LogError("养殖信息修改失败");
                res.Num=StateCode.Fail;
                res.Message="修改失败";
           
            }
            return res;
            
        }
    }
}
